Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

392
Visualizações
Data should be a "String", "Array of arrays" OR "Array of objects" when trying to create csv file

I am trying to download csv file of table entries using react-csv.

But every time I try to follow the steps to download the data and export them to CSV format I get this error:

Data should be a "String", "Array of arrays" OR "Array of objects"

enter image description here

here is my code:

    import React, { useEffect, useRef, useState } from "react";
    import { CSVLink } from "react-csv";

    export default function App() {
    const [data, setdata] = React.useState("");
    const [tableCellsData, setTableCellsData] = useState([]);
    const csvLink=useRef()
    const [bDownloadReady, setDownloadReady] = useState(false);

    useEffect(() => {
        if (csvLink && csvLink.current && bDownloadReady) {
            csvLink.current.link.click();
            setDownloadReady(false);
        }
    }, [bDownloadReady]);

     const getUniqueKeyFromArrayIndex = (rowNum, columnNum) => {
     return `${rowNum}-${columnNum}`;
     };

    const onChangeHandler = (e) => {
    console.log(e.target.name, e.target.value);

    setTableCellsData({
      ...tableCellsData,
      [e.target.name]: e.target.value
     });
    };

    const generateTable = () => {
    let table = [];
    for (let i = 0; i < 3; i++) {
      let children = [];
      for (let j = 0; j < 4; j++) {
        children.push(
          <td>
            <input
              name={getUniqueKeyFromArrayIndex(i, j)}
              defaultValue=""
              Value={data}
              onChange={onChangeHandler}
            />
          </td>
        );
      }
      table.push(
        <tr key={i}>
          <td>{children}</td>
        </tr>
      );
    }
    return table;
    };

  
     const tabled=Object.keys(tableCellsData)


     function clickHandler(actionType){
       if (actionType === 'DOWNLOAD') {
         setDownloadReady(true);
       }
     }
  

     return (
     <>
        <table>{generateTable()}</table>
        <button onClick={(e)=>clickHandler('DOWNLOAD')}>click</button>
        <CSVLink 
                    data={tabled} 
                    filename="data.csv"
                    className="hidden"
                    ref={csvLink}
                    target="_blank" />
       </>
     );
    }

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This code:

const tabled=Object.keys(tableCellsData)

will produce an array of strings (keys). Following the error it looks like this format is not supported. You need to pass an array of objects or arrays, or a string, but an array of string its not supported

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda